﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using HalconDotNet;
using NDK.Module.BLL;
using NDK.Motion.BLL;
using NDK.Motion.Sys;
using NDK.Utils;

namespace NDK.Frm
{
    public partial class Dlg_Display : Form
    {
        HWindow hWindow;
        Timer timer;
        BackgroundWorker bgw;

        HTuple width, height;
        private HImage hImage { get; set; } = new HImage();

        public Dlg_Display()
        {
            InitializeComponent();
            hWindow = hWindowControl1.HalconWindow;
            hWindowControl1.HalconWindow.SetColor("red");
            hWindowControl1.HalconWindow.SetDraw("margin");
            hWindowControl1.HalconWindow.SetLineWidth(1);
        }

        private void DisplayImage(HImage image)
        {
            try
            {
                HOperatorSet.DispObj(image, hWindow);
            }
            catch { }
        }


        private void Frm_Display_Load(object sender, EventArgs e)
        {
            foreach (var item in Machine.DicCamera.Keys)
                cbb.Items.Add(item);
            if (cbb.Items.Count > 0)
            {
                cbb.Text = cbb.Items[0].ToString();
            }
            DisplatExp = Convert.ToDouble(nudExp.Value);
            timer = new Timer();
            timer.Interval = 20;
            timer.Tick += Timer_Tick;
            timer.Enabled = true;

            bgw = new BackgroundWorker();
            bgw.WorkerSupportsCancellation = true;
            bgw.DoWork += Bgw_DoWork;
        }

        private void Bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!(MachineStatusManager.CurrentMachineStatus == MachineStatus.Running || MachineStatusManager.CurrentMachineStatus == MachineStatus.Pause))
            {
                if (btDisplay.Text == "关闭实时")
                {
                    hImage = PublicMethod.GradImage(cbb.Text, DisplatExp);
                    if (hImage != null)
                        DisplayImage(hImage);
                }
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (cbb.Items.Count != 0)
            {
                if (bgw != null && !bgw.IsBusy) bgw.RunWorkerAsync();
            }
        }

        private void Frm_Display_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer.Enabled = false;
            if (btDisplay.Text == "关闭实时")
            {
                MessageBox.Show("请关闭实时,再关闭窗口");
                e.Cancel = true;
            }
        }

        private void btDisplay_Click(object sender, EventArgs e)
        {
            if (!(MachineStatusManager.CurrentMachineStatus == MachineStatus.Running || MachineStatusManager.CurrentMachineStatus == MachineStatus.Pause)
                && cbb.Items.Count != 0)
            {
                if (btDisplay.Text == "关闭实时")
                {
                    btDisplay.Text = "打开实时";
                }
                else
                {
                    btDisplay.Text = "关闭实时";
                }
            }
        }

        private void btPhoto_Click(object sender, EventArgs e)
        {
            if (!(MachineStatusManager.CurrentMachineStatus == MachineStatus.Running || MachineStatusManager.CurrentMachineStatus == MachineStatus.Pause)
               && cbb.Items.Count != 0)
            {
                if (btDisplay.Text == "关闭实时")
                {
                    MessageBox.Show("请关闭实时，再单次拍照");
                    return;
                }
                PublicMethod.writeLog($"定位相机拍照开始", "定位相机");
                hImage = PublicMethod.GradImage(cbb.Text, DisplatExp);
                PublicMethod.writeLog($"定位相机拍照结束", "定位相机");
                if (hImage != null)
                    DisplayImage(hImage);
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (hImage != null)
            {
                SaveFileDialog saveFile = new SaveFileDialog();
                saveFile.Filter = "(*.bmp)|*.bmp";
                if (DialogResult.OK == saveFile.ShowDialog())
                {
                    HOperatorSet.WriteImage(hImage, "bmp", 0, saveFile.FileName);
                }
            }
        }

        private void cbb_SelectedIndexChanged(object sender, EventArgs e)
        {
            hImage = PublicMethod.GradImage(cbb.Text);
            HOperatorSet.GetImageSize(hImage, out width, out height);
            HOperatorSet.SetPart(hWindow, 0, 0, height, width);
        }

        double DisplatExp = 10;
        private void nudExp_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                DisplatExp = Convert.ToDouble(nudExp.Value);
            }
            catch (Exception)
            {
                DisplatExp = 10;
            }
        }

        private void hswc_HMouseWheel(object sender, HMouseEventArgs e)
        {
            try
            {
                if (hImage != null)
                {
                    HTuple Zoom, Row, Col, Button;
                    HTuple Row1, Col1, Row2, Col2, Ht, Wt, R1, C1, R2, C2;
                    if (e.Delta > 0) Zoom = 1.5;
                    else Zoom = 0.5;

                    HOperatorSet.GetMposition(hWindow, out Row, out Col, out Button);

                    HOperatorSet.GetPart(hWindow, out Row1, out Col1, out Row2, out Col2);

                    Ht = Row2 - Row1;
                    Wt = Col2 - Col1;
                    if (Ht * Wt < 32000 * 32000 || Zoom == 1.5)
                    {
                        R1 = (Row1 + ((1 - (1 / Zoom)) * (Row - Row1)));
                        C1 = (Col1 + ((1 - (1 / Zoom)) * (Col - Col1)));
                        R2 = R1 + (Ht / Zoom);
                        C2 = C1 + (Wt / Zoom);
                        HOperatorSet.SetPart(hWindow, R1, C1, R2, C2);
                        HOperatorSet.ClearWindow(hWindow);

                        if (btDisplay.Text == "实时显示打开")
                        {
                            HOperatorSet.DispObj(hImage, hWindow);
                        }
                        else
                        {
                            HOperatorSet.DispObj(hImage, hWindow);
                        }
                    }
                }
            }
            catch { }
        }
    }
}
